package com.leyou.user.web;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/info")
public class UserController {
    @Autowired
    private UserService userService;


    //查看用户是否存在
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> exists123(
            @PathVariable("data") String data,
            @PathVariable("type") Integer type
    ) {
        return ResponseEntity.ok(this.userService.exists(data, type));
    }

    //短信验证功能
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
        userService.sendCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //注册用户
    @PostMapping
    public ResponseEntity<Void> userRegister(
             User user,
            @RequestParam("code") String code) {

        this.userService.register(user, code);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
     //根据用户名和密码查询
    @GetMapping
    public ResponseEntity<UserDTO> queryUserByNameAndPass(
            @RequestParam("username")String username,
            @RequestParam("password")String password){
        return ResponseEntity.ok(this.userService.queryUserByNameAndPass(username,password));
    }

    @GetMapping("/me")
    public ResponseEntity<UserDetail> checkLogin(){
        return ResponseEntity.ok(UserContext.getUser());
    }



}
